function [negative_path,positive_path]=EEG_pretreatment(main_folder_path, in_periodization_path, end_subject_num, ...
    band_pass_start_freq,band_pass_end_freq,notch_start_freq,notch_end_freq)
    eeglab('nogui');
    % 指定文件夹路径
    %main_folder_path = 'C:\Users\liwan\Desktop\EDF_File\new-chb-mit\';
    % 分期表
    %in_periodization_path='CHBMIT_seizures_new_15_1';
    periodization_data = readtable(in_periodization_path);
    tolerance = 1e-10;
    subfolders = dir(main_folder_path);
    subfolders = subfolders([subfolders.isdir]);  % 仅保留文件夹
    
    
    % 循环处理每个子文件夹
    for folder_index = 1:length(subfolders)
        folder_name = subfolders(folder_index).name;
        numeric_part = regexp(folder_name, '\d+', 'match');
        %% 取chb01-chb20
        if startsWith(folder_name, 'chb') && str2double(numeric_part{1}) <= end_subject_num
            % 构造子文件夹的完整路径
            folder_path = fullfile(main_folder_path, folder_name);
            
            % 检查子文件夹下是否有.edf文件
            file_list = dir(fullfile(folder_path, '*.edf'));
            % 循环处理每个文件
            for file_index = 1:length(file_list)
                % 构建文件的完整路径
                file_path = fullfile(folder_path, file_list(file_index).name);
            
                filename=getFileName(file_path);
                EEG = pop_biosig(file_path);
            
                %滤波
                %--------------- START -----------------
                %带通
                EEG = pop_eegfiltnew(EEG,'locutoff',band_pass_start_freq,'hicutoff',band_pass_end_freq);
                %陷波
                EEG = pop_eegfiltnew(EEG, 'locutoff',notch_start_freq,'hicutoff',notch_end_freq,'revfilt',1);
                %---------------- END ------------------
                %% 调整21通道数据 需要注释
                %检验通道数是否满足
                if(size(EEG.data, 1) < 18)
                    disp(filename);
                    break;
                end
                %
                if~isempty(periodization_data(contains(periodization_data.FileName, filename), :))
                    matchingRecords = periodization_data(contains(periodization_data.FileName, filename), :);
                else
                    continue;
                end
                %EEG=ft_read_data(filepath);
                %默认排序正确 第一段数据段的开始时间为文件开始时间
                file_start_time = matchingRecords(1,:).StartTime{1};
                negative_indices_array=[];
                positive_indices_array=[];
                for i =1:height(matchingRecords)
                    if(strcmp(matchingRecords(i,:).type, {'interictal'}))
                        [start_time,end_time]=timeToIndices(EEG,file_start_time,matchingRecords(i,:).StartTime,matchingRecords(i,:).FrameNumbers);
                        negative_indices_array=[negative_indices_array;start_time,end_time];
                    elseif(strcmp(matchingRecords(i,:).type, {'preictal'}))
                        [start_time,end_time]=timeToIndices(EEG,file_start_time,matchingRecords(i,:).StartTime,matchingRecords(i,:).FrameNumbers);
                        positive_indices_array=[positive_indices_array;start_time,end_time];
                    end
                end
                positive_path = fullfile(pwd, 'positive');
                negative_path = fullfile(pwd, 'negative');
                new_filename = erase(filename, '.edf');
                if ~exist(negative_path, 'dir')
                    mkdir(negative_path);
                end
                if ~exist(positive_path, 'dir')
                    mkdir(positive_path);
                end
                if(height(negative_indices_array)||height(positive_indices_array))
                    %negative_indices_array 处理
                    for i=1:height(negative_indices_array)
                        start_index = negative_indices_array(i,1);
                        end_index = negative_indices_array(i,2);
                        data = EEG.data(:,start_index:end_index);
                        %% 用于调整21通道数据
                        %data=filter_data(data,tolerance);
                        filename = [new_filename,'_negative_',num2str(i),'.mat'];
                        save(fullfile(negative_path,filename),'data');
                    end
                    %positive_indices_array 处理
                    for i=1:height(positive_indices_array)
                        start_index = positive_indices_array(i,1);
                        end_index = positive_indices_array(i,2);
                        data = EEG.data(:,start_index:end_index);
                        %% 用于调整21通道数据
                        %data=filter_data(data,tolerance);
                        filename = [new_filename,'_positive_',num2str(i),'.mat'];
                        save(fullfile(positive_path,filename),'data');
                    end
                end
            end
         end
    end
    
    function filename = getFileName(filepath)
        [~, filename, ext] = fileparts(filepath);
        %适配MIT 华山暂时注释
        filename = strrep(filename, '_reduced', '');
        filename = fullfile([filename, ext]);
    end
    function [startIndex,endIndex] = timeToIndices(eegObj,fileStartTime,startTime, frameNumbers)
        % 输入：
        % - eegObj: EEG对象 包含文件各信息
        % - fileStartTime: 文件的开始时间，格式 'HH:mm:ss'
        % - startTime: 需要映射的开始时间字符串，格式 'HH:mm:ss'
        % - frameNumbers: 帧数 持续时间*采样率
    
    
        % 将时间字符串转换为 datetime 对象
        startTimeDT = datetime(startTime, 'Format', 'HH:mm:ss');
        
        timeDifference = startTimeDT - datetime(fileStartTime, 'Format', 'HH:mm:ss');
    
        if seconds(timeDifference) < 0
            % 加上一整天的秒数
            timeDifference = timeDifference + days(1);
        end
        startTimeSeconds = seconds(timeDifference);
        % 计算在数组中对应的位置
        startIndex = round(startTimeSeconds * eegObj.srate) + 1;
        %% 终点时间表示"终点时间-1"个采样点（右侧闭区间 减1操作）
        endIndex = startIndex+frameNumbers-1*eegObj.srate -1;
    
        % 保证索引在数组范围内
        startIndex = max(1, startIndex);
        endIndex = min(eegObj.pnts, endIndex);
    end
end
%% 用于调整21通道数据
function data_filtered = filter_data(data, tolerance)
    % 过滤小于tolerance的数据
    rows_to_keep = ~(all(abs(data) < tolerance, 2));
    data_filtered = data(rows_to_keep, :);
    %% 类型1处理
    
    % 截取前22条数据
    if size(data_filtered, 1) >= 22
        data_filtered = data_filtered(1:22, :);
    else
        data_filtered = data_filtered(1:end, :);
    end
    
    % 删除第19条数据
    if size(data_filtered, 1) >= 19
        data_filtered(19, :) = [];
    end
    
    %% 补充类型2处理
     % 将第9和第10条数据移动至第17和第18位置，其他上移
    if size(data_filtered, 1) >= 18
        % 保存第9和第10条数据
        temp9 = data_filtered(9, :);
        temp10 = data_filtered(10, :);
        
        % 上移第11到第16条数据到第9到第14位置
        data_filtered(9:16, :) = data_filtered(11:18, :);
        
        % 插入第9和第10条到第17和第18条位置
        data_filtered(17, :) = temp9;
        data_filtered(18, :) = temp10;
    end
    
end
